package com.bana.fvs.app.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;

import com.bana.fvs.R;
import com.wei.commonlibrary.app.AppManager;
import com.wei.commonlibrary.mvp.presenter.CommPresenter;
import com.wei.commonlibrary.titlebar.BaseTitleBarView;
import com.wei.commonlibrary.utils.TUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

//import com.zhy.autolayout.AutoFrameLayout;
//import com.zhy.autolayout.AutoLinearLayout;
//import com.zhy.autolayout.AutoRelativeLayout;


/**
 * Author : yanftch
 * Date : 2018/2/6
 * Time : 11:27
 * Desc : Activity基类
 */

public abstract class BaseActivity<P extends CommPresenter> extends AppCompatActivity implements View.OnClickListener, BaseTitleBarView.TitleBarClick {
    private static String TAG = "";
    public Context mContext;
    public BaseActivity mActivity;
    private ViewGroup mWindowRootLayout;
    protected BaseTitleBarView mBaseTitleBarView;
    private Unbinder mUnbinder;

    protected P mPresenter;

    private static final String LAYOUT_LINEARLAYOUT = "LinearLayout";
    private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
    private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";
    public static int mWindowSlideInAni;
    public static int mWindowSlideOutAni;

    //
//    @Override
//    public View onCreateView(String name, Context context, AttributeSet attrs) {
//        View view = null;
//        if (name.equals(LAYOUT_FRAMELAYOUT)) {
//            view = new AutoFrameLayout(context, attrs);
//        }
//
//        if (name.equals(LAYOUT_LINEARLAYOUT)) {
//            view = new AutoLinearLayout(context, attrs);
//        }
//
//        if (name.equals(LAYOUT_RELATIVELAYOUT)) {
//            view = new AutoRelativeLayout(context, attrs);
//        }
//
//        if (view != null) return view;
//        return super.onCreateView(name, context, attrs);
//    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);// 取消标题
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        mContext = this;
        mActivity = this;
        mWindowSlideInAni = R.anim.open_from_right;
        mWindowSlideOutAni = R.anim.close_from_right;

        AppManager.getAppManager().addActivity(this);
        TAG = "dah_" + mContext.getClass().getSimpleName();
        Log.e(TAG, "onCreate.activityName = " + getClass().getSimpleName());
        int resId = setLayout();
        View view = View.inflate(this, resId, null);
        setContentView(resId);
        mUnbinder = ButterKnife.bind(this);
        mPresenter = TUtil.getT(this, 0);
        initPresenter();
        initTitle();
        setTitle();
        initWidget();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.base_layout);
        mWindowRootLayout = (ViewGroup) findViewById(R.id.window_root_layout);

        mBaseTitleBarView = (BaseTitleBarView) findViewById(R.id.base_title_bar);

        View contentView = LayoutInflater.from(this).inflate(layoutResID, null);
        mWindowRootLayout.addView(contentView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        if (null != mBaseTitleBarView) {
            mBaseTitleBarView.setTitleBarClick(this);
            mBaseTitleBarView.setLeftContainerVisible();
        }
    }

    protected <T extends View> T findView(int resId) {
        return (T) (findViewById(resId));
    }

    private void initTitle() {
    }


    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    /**
     * 布局ID
     *
     * @return
     */
    public abstract int setLayout();

    /**
     * 设置标题显示的内容
     */
    public abstract void setTitle();

    /**
     * 初始化控件
     */
    public abstract void initWidget();

    /**
     * 添加事件
     */
    public abstract void widgetClick(View v);

    /**
     * 设置view回调
     */
    protected abstract void initPresenter();

    @Override
    public void onTitlePressed() {

    }

    @Override
    public void onTitleLeftPressed() {

    }

    @Override
    public void onTitleRightTextPressed() {

    }

    /**
     * ---------------------------生命周期---------------------------
     */
    @Override
    protected void onResume() {
        super.onResume();
        mContext = this;
        mActivity = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void finish() {
        super.finish();
        AppManager.getAppManager().removeActivity(this);
        tryNavigateBackDefaultTransition();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
        mContext = null;
        if (mUnbinder != null) {
            mUnbinder.unbind();
            mUnbinder = null;
        }
//        if (mPresenter != null) {
//            mPresenter.onDestory();
//            mPresenter = null;
//        }
        Log.e(TAG, "onDestroy:  = ");
        mActivity = null;
        System.gc();
    }


    public void startActivityAndFinish(Class<?> clazz) {
        startActivityAndFinish(clazz, null);
    }

    public void startActivityAndFinish(Class<?> clazz, Bundle bundle) {
        startActivity(clazz, bundle);
        this.finish();
    }

    public void startActivity(Class<?> clazz) {
        startActivity(clazz, null);
    }

    public void startActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent();
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        intent.setClass(getApplicationContext(), clazz);
        startActivity(intent);
        tryNavigateDefaultTransition();

        //设置切换动画
//        overridePendingTransition();
    }

    private void tryNavigateDefaultTransition() {
        if (mWindowSlideInAni != 0) {
            overridePendingTransition(mWindowSlideInAni, 0);
        }
    }

    private void tryNavigateBackDefaultTransition() {
        if (mWindowSlideOutAni != 0) {
            overridePendingTransition(0, mWindowSlideOutAni);
        }
    }
}
